
package com.phevos.web.dao.ora;

import java.io.BufferedWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import oracle.sql.CLOB;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.phevos.common.dbpool.TransactionManager;
import com.phevos.web.db.BaseDAO;
import com.phevos.web.util.DBUtil;

public class InsertClobFieldDAO extends BaseDAO
{
    private static Logger logger = LoggerFactory.getLogger(InsertClobFieldDAO.class);

    public InsertClobFieldDAO()
    {
    }

    /**
     * ����Clob�ֶε����,��������������ֶε���� �����ļ��Ĳ���Ҫ�� һ��sql����ܲ�ѯ��Ҫ����ļ�¼��
     * sql������ʹ��ռλ��"?"
     * һ��update�����Ҫ���ԭ�е����,}��sql������ʹ����ͬ��ռλ��"?",
     * ��ռλ��"?"������ͬ,��Ӧ˳���ֵ��ͬ �����Ĳ���Ҫ��
     * ��һ�����Ϊclob�ֶ���ڶ������Ϊclob�ֶε����� ����Ĳ�������sql����ռλ�����Ӧ
     * 
     * @param args Object[]
     * @return List
     */
    public List query(Object[] args)
    {
        String select_sql = (String)params.get("select-sql");
        String update_sql = (String)params.get("update-sql");
        if (select_sql == null || update_sql == null)
        {
            logger.error("����sqlû������");
            return null;
        }
        select_sql = select_sql.trim();
        update_sql = update_sql.trim();
        if (args == null || args.length < 2)
        {
            return null;
        }
        String filedName = args[0].toString();
        String fieldValue = args[1].toString();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        BufferedWriter out = null;
        TransactionManager transMgr = null;
        try
        {
            transMgr = new TransactionManager();
            transMgr.beginTransaction();
            conn = transMgr.getConnection();
            // ����ֶ�ԭ�е����,ִ��update���
            /**
             *   args[0] = "text";
        args[1] = text;
        args[2] = new Integer(art_id);
        select text from tbfzyw_kb_articles where art_id=? for update
        update tbfzyw_kb_articles set text = EMPTY_CLOB() where art_id=?
             */
            stmt = conn.prepareStatement(select_sql);
            //TODO  ��Ӧ������д�ɣ�
            for (int i = 2; i < args.length; i++)
            {
                stmt.setObject(1, args[i]);
            }
            stmt.executeQuery();
            // �����µ����,ִ��select���
            stmt = conn.prepareStatement(update_sql);
            for (int i = 2; i < args.length; i++)
            {
                stmt.setObject(1, args[i]);
            }
            rs = stmt.executeQuery();
            if (rs.next())
            {
                rs = stmt.executeQuery(select_sql.toString());
                while (rs.next())
                {
                    try
                    {
                        CLOB clob = (CLOB)rs.getClob(filedName);
                        out = new BufferedWriter(clob.getCharacterOutputStream());
                        out.write(fieldValue);
                        out.flush();
                        out.close();
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                    finally
                    {
                        if (out != null)
                        {
                            out.close();
                        }
                    }
                }
            }
            transMgr.commit();
        }
        catch (Exception e)
        {
            transMgr.cancelTransaction();
            logger.error("",e);
        }
        finally
        {
            DBUtil.closeResultSet(rs);
            DBUtil.closeStatement(stmt);
            transMgr.endTransaction();
        }
        return null;
    }
}
